<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="印章名称" prop="sealName">
        <el-input
          v-model="queryParams.sealName"
          placeholder="请输入印章名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="jumpDZQ"
          v-hasPermi="['system:sealInfo:add']"
        >去腾讯签管理</el-button>
      </el-col>
    </el-row>

    <!-- <el-table v-loading="loading" :data="sealInfoList">
      <el-table-column :formatter="defaultTableColumnDate" label="序号" align="center" type="index" />
      <el-table-column :formatter="defaultTableColumnDate" label="印章名称" align="center" prop="sealName" />
      <el-table-column :formatter="defaultTableColumnDate" label="印章类型" align="center" prop="sealType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.seal_type" :value="scope.row.sealType"/>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="更新人" align="center" prop="updateUser" />
      <el-table-column :formatter="defaultTableColumnDate" label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sealInfo:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleForm(scope.row)"
            v-hasPermi="['system:sealInfo:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改企业印章管理对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="印章名称" prop="sealName">
          <el-input v-model="form.sealName" placeholder="请输入印章名称" />
        </el-form-item>
        <el-form-item label="印章类型" prop="sealType">
          <el-select v-model="form.sealType" placeholder="请选择印章类型" style="width: 100%;">
            <el-option
              v-for="item in dict.type.seal_type"
              :key="item.id"
              :label="item.label"
              :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="印章图片" prop="sealPhoto">
          <image-upload :limit="1" v-model="form.sealPhoto"/>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog destroy-on-close :title="title" :visible.sync="formopen" width="500px" append-to-body>
      <div style="text-align: center;">
        <image-preview :src="form.sealPhoto" :width="200" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { authenticationOrConsole } from "@/api/system/contract";
import { listSealInfo, getSealInfo, delSealInfo, addSealInfo, updateSealInfo, getSealUrl } from "@/api/system/sealInfo";

export default {
  name: "SealInfo",
  dicts: ['seal_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业印章管理表格数据
      sealInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      formopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sealName: null,
        sealType: null,
        sealPhoto: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async imgToBase64(url) {
      return new Promise((resolve, reject) => {
        const image = new Image()
        image.src = url
        image.onload = () => {
          const canvas = document.createElement('canvas')
          canvas.width = image.naturalWidth // 使用 naturalWidth 为了保证图片的清晰度
          canvas.height = image.naturalHeight
          canvas.style.width = `${canvas.width / window.devicePixelRatio}px`
          canvas.style.height = `${canvas.height / window.devicePixelRatio}px`
          const ctx = canvas.getContext('2d')
          if (!ctx) {
            return null
          }
          ctx.drawImage(image, 0, 0)
          const base64 = canvas.toDataURL('image/png')
          return resolve(base64)
        }
        image.onerror = (err) => {
          return reject(err);
        }
      })
    },
    /** 查询企业印章管理列表 */
    getList() {
      this.loading = true;
      listSealInfo(this.queryParams).then(response => {
        this.sealInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.formopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sealName: null,
        sealType: null,
        sealPhoto: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业印章管理";
    },
    async jumpDZQ() {
      const res = await authenticationOrConsole({})
      window.open(res.data.pc)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSealInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业印章管理";
      });
    },
    handleForm(row) {
      this.reset();
      const id = row.id || this.ids
      getSealUrl({id}).then(response => {
        if(response.code == 200) {
          this.form = {sealPhoto: response.msg};
          this.formopen = true;
          this.title = "查看印章";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          // this.form.sealPhoto = await this.imgToBase64(process.env.VUE_APP_BASE_API + this.form.sealPhoto)
          if (this.form.id != null) {
            updateSealInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSealInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业印章管理编号为"' + ids + '"的数据项？').then(function() {
        return delSealInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sealInfo/export', {
        ...this.queryParams
      }, `sealInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
